package org.yar.activities;

import org.yar.R;
import org.yar.common.AmbilWarnaPreference;
import org.yar.common.EncodingUtils;
import org.yar.common.TextSizePreference;

import android.graphics.Color;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.text.method.PasswordTransformationMethod;

public class YARPreferencesActivity 
	extends PreferenceActivity {
	
	public static final String PREF_DEF_ENCODING = "defEncoding";
	public static final String PREF_TEXT_COLOR = "textColor";
	public static final String PREF_BG_COLOR = "bgColor";
	public static final String PREF_TEXT_SIZE = "textSize";
	public static final String PREF_USE_PASSWD = "usePassword";
	public static final String PREF_PASSWD = "password";
	
	private ListPreference defEncPreference;
	private AmbilWarnaPreference textColorPreference;
	private AmbilWarnaPreference bgColorPreference;
	private TextSizePreference textSizePreference;
	private CheckBoxPreference usePasswordPreference;
	private EditTextPreference passwordPreference;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setPreferenceScreen(createPreferenceHierarchy());
        setPreferenceDependencies();
    }
	
	private PreferenceScreen createPreferenceHierarchy() {
		PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
		
		PreferenceCategory textCategory = new PreferenceCategory(this);
		textCategory.setTitle(R.string.pref_cat_text);
		root.addPreference(textCategory);
		
		/* Default encoding preference */
		defEncPreference = new ListPreference(this);
		defEncPreference.setKey(PREF_DEF_ENCODING);
		defEncPreference.setTitle(R.string.pref_def_encoding_title);
		defEncPreference.setSummary(R.string.pref_def_encoding_summary);
		defEncPreference.setDialogTitle(R.string.pref_def_encoding_dialog_title);
		defEncPreference.setEntries(EncodingUtils.getEncodings());
		defEncPreference.setEntryValues(EncodingUtils.getEncodings());
		defEncPreference.setDefaultValue("UTF-8");
		textCategory.addPreference(defEncPreference);
		
		/* Text color preference */
		textColorPreference = new AmbilWarnaPreference(this);
		textColorPreference.setTitle(R.string.pref_text_color_title);
		textColorPreference.setSummary(R.string.pref_text_color_summary);
		textColorPreference.setKey(PREF_TEXT_COLOR);
		textColorPreference.setDefaultValue(Color.WHITE);
		textCategory.addPreference(textColorPreference);
		
		/* Background color preference */
		bgColorPreference = new AmbilWarnaPreference(this);
		bgColorPreference.setTitle(R.string.pref_bg_color_title);
		bgColorPreference.setSummary(R.string.pref_bg_color_summary);
		bgColorPreference.setKey(PREF_BG_COLOR);
		bgColorPreference.setDefaultValue(Color.BLACK);
		textCategory.addPreference(bgColorPreference);
		
		/* Text size preference */
		textSizePreference = new TextSizePreference(this);
		textSizePreference.setTitle(R.string.pref_text_size_title);
		textSizePreference.setSummary(R.string.pref_text_size_summary);
		textSizePreference.setKey(PREF_TEXT_SIZE);
		textCategory.addPreference(textSizePreference);
		
		PreferenceCategory readerCategory = new PreferenceCategory(this);
		readerCategory.setTitle(R.string.pref_cat_security);
		root.addPreference(readerCategory);
		
		/* Use password preference */
		usePasswordPreference = new CheckBoxPreference(this);
		usePasswordPreference.setTitle("Use password");
		usePasswordPreference.setSummary("Set whether to use a password to lock this application.");
		usePasswordPreference.setKey(PREF_USE_PASSWD);
		readerCategory.addPreference(usePasswordPreference);
		
		/* Password preference */
		passwordPreference = new EditTextPreference(this);
		passwordPreference.getEditText().setTransformationMethod(new PasswordTransformationMethod());
		passwordPreference.setTitle("Password");
		passwordPreference.setSummary("Set the password used to lock this application.");
		passwordPreference.setKey(PREF_PASSWD);
		//passwordPreference.setDependency(PREF_USE_PASSWD);
		
		readerCategory.addPreference(passwordPreference);
		
		return root;
	}
	
	protected void setPreferenceDependencies()
    {
        passwordPreference.setDependency(PREF_USE_PASSWD);
    }
}
